package middle;

import java.util.ArrayList;
import java.util.List;

public class Solution_92 {

    public ListNode reverseBetween(ListNode head, int left, int right) {
        List<ListNode> list = new ArrayList<>();
        ListNode node = head;
        while (node != null) {
            list.add(node);
            ListNode temp = node;
            node = node.next;
            temp.next = null;
        }
        for (int i = left - 1, j = right - 1; i < j; i++, j--) {
            ListNode temp = list.get(i);
            list.set(i, list.get(j));
            list.set(j, temp);
        }
        for (int i = 0; i < list.size() - 1; i++) {
            list.get(i).next = list.get(i + 1);
        }
        return list.get(0);
    }

    class ListNode {

        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
